package de.fmannan.wildit.domainmodel;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarConverter {

	/**
	 * Converts a given Calendar object to the given string.
	 * If the {@link Calendar} object could not be parsed using the
	 * provided format, an empty String is returned.
	 */
	public static String CalendarToString(Calendar calendar, String format)
	{
		String result = "";
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatter.format(calendar.getTime());
		
		return result;		
	}
	
	/**
	 * Parses a time string which matches the given format.
	 * 
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Calendar StringToCalendar(String time, String format) throws java.text.ParseException {
		
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		Date dateTime = formatter.parse(time);
		calendar.setTime(dateTime);
		return calendar;
	}

}
